package design;

public class client {

    //抽象组件
    static abstract class Component{
        public abstract void operation();
    }

    //具体组件
    static class ConcreteComponent extends Component{
        public void operation() {
            System.out.println("处理业务逻辑");
        }
    }

    static abstract class Decorator extends Component{
        //装饰的组件
        protected Component component;

        //构造方法传入组件对象
        public Decorator(Component component) {
            this.component = component;
        }

        public void operation(){
            //转发请求给组件对象，可以在转发前后执行一些附加动作
            component.operation();
        }
    }

    static class ConcreteDecoratorA extends Decorator{

        public ConcreteDecoratorA(Component component) {
            super(component);
        }

        private void operationFirst(){
            System.out.println("operationFirst");
        }

        private void operationLast(){
            System.out.println("operationLast");
        }

        public void operation() {
            operationFirst();
            super.operation();
            operationLast();
        }
    }

    public static void main(String[] args) {
        Component c1 = new ConcreteComponent();
        Decorator decorator = new ConcreteDecoratorA(c1);
        decorator.operation();
    }
}
